﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visiblox.Charts;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using IbsysII.Util;
using System.Drawing;

namespace IbsysII.Domain
{
   public  class Dashboard
    {
        public double produktivitaet;
        public double liefertreue;
        public int absatzAktPeriode;
        public int absatzAllePerioden;
        public int absatzFolgePeriode;
        public double umsatzrendite;

    }

   public class ChartUmsatz {

       public Chart loadUmsatzChart(Chart pChart) {
         pChart.Series.Clear();
        StackedColumnSeries stack = new StackedColumnSeries();
        List<ColumnSeries> lstColSeries = new List<ColumnSeries>();
        List<DataSeries<string, double>> lstDS = new List<DataSeries<string, double>>();
        List<DataPoint<string, double>> lstDP = new List<DataPoint<string, double>>();
        LineSeries ls = new LineSeries();

        List<int> absatz = new List<int>();
        string umsatz;

        App.db = Xpath_Zugriff.getDashboardWerte();
        if (App.boolActualLanguage == false)
        {
            umsatz = "Umsatz";
        }
        else {
            umsatz = "Turnover";
        }

        try
        {
            absatz.Add(App.vertrieb[0].Bedarf);
            absatz.Add(App.vertrieb[1].Bedarf);
            absatz.Add(App.vertrieb[2].Bedarf);

        }

        catch (Exception e) { }

        DataSeries<string,double> ds2 = new DataSeries<string,double>();

        DataPoint<string, double> dp1 = new DataPoint<string, double>();
        DataPoint<string, double> dp2 = new DataPoint<string, double>();
        DataPoint<string, double> dp3 = new DataPoint<string, double>();
        DataPoint<string, double> dp4 = new DataPoint<string, double>();
        DataPoint<string, double> dp5 = new DataPoint<string, double>();
        DataPoint<string, double> dp6= new DataPoint<string, double>();
        DataPoint<string, double> dp7 = new DataPoint<string, double>();
        DataPoint<string, double> dp8 = new DataPoint<string, double>();

        List<string> stackAxisLbl = new List<string>();
        stackAxisLbl.Add(umsatz + " n-2");
        stackAxisLbl.Add("P1(n-1)");
        stackAxisLbl.Add("P2(n-1)");
        stackAxisLbl.Add("P3(n-1)");
        stackAxisLbl.Add(umsatz + " n-1");
        stackAxisLbl.Add("P1(n)");
        stackAxisLbl.Add("P2(n)");
        stackAxisLbl.Add("P3(n)");
        stackAxisLbl.Add(umsatz + " n");

        dp1.X = "Test0";
        dp2.X = "Test1";
        dp3.X = "Test2";
        dp4.X = "Test3";
        dp5.X = "Test3";
        dp6.X = "Test3";
        dp7.X = "Test3";
        dp8.X = "Test3";

        dp1.Y = 2050;
        dp2.Y = 2050;
        dp3.Y = 2050;
        dp4.Y = 2050;
        dp5.Y = 2050;
        dp6.Y = 2050;
        dp7.Y = 2050;
        dp8.Y = 2050;

        ds2.Add(dp1);
        ds2.Add(dp2);
        ds2.Add(dp3);
        ds2.Add(dp4);
        ds2.Add(dp5);
        ds2.Add(dp6);
        ds2.Add(dp7);
        ds2.Add(dp8);

        ls.DataSeries = ds2;


        for (int i = 0; i <= 9; i++) {
            ColumnSeries cs = new ColumnSeries();
            DataSeries<string, double> ds = new DataSeries<string, double>();

            if (i == 0) {
                cs.Visibility = System.Windows.Visibility.Hidden;
            }

            for (int j = 0; j <= 8; j++)
            {
                if (i == 1 && j == 0)
                {
                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAllePerioden*200;
                    ds.Add(dp);
                }
                
                else if (i == 2 && j == 1)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAktPeriode / 3 * 200;
                    ds.Add(dp);
                }
                else if (i == 0 && j == 1)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAllePerioden * 200;
                    ds.Add(dp);
                }

                else if (i == 0 && j == 2)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAllePerioden * 200 + (App.db.absatzAktPeriode / 3 * 200);
                    ds.Add(dp);
                }

                else if (i == 0 && j == 3)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAllePerioden * 200 + (App.db.absatzAktPeriode / 3 * 200) + (App.db.absatzAktPeriode / 3 * 200);
                    ds.Add(dp);
                }

                else if (i == 3 && j == 2)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAktPeriode / 3 * 200;
                    ds.Add(dp);
                }
                else if (i == 4 && j == 3)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAktPeriode / 3 * 200;
                    ds.Add(dp);
                }
                else if (i == 5 && j == 4)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAktPeriode * 200;
                    ds.Add(dp);
                }
                else if (i == 1 && j == 4)
                {

                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = App.db.absatzAllePerioden * 200;
                    ds.Add(dp);
                }

                else if (i == 6 && j == 5)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = absatz[0] * 200;
                        ds.Add(dp);
                    }
                    catch(Exception e){};
                }
                else if (i == 0 && j == 5)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = App.db.absatzAllePerioden * 200 + App.db.absatzAktPeriode * 200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }
                else if (i == 7 && j == 6)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = absatz[1]*200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }
                else if (i == 0 && j == 6)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = App.db.absatzAllePerioden * 200 + App.db.absatzAktPeriode * 200 + absatz[0] * 200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }
                else if (i == 8 && j == 7)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = absatz[2] * 200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }
                else if (i == 0 && j == 7)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = App.db.absatzAllePerioden * 200 + App.db.absatzAktPeriode * 200 + absatz[0] * 200 + absatz[1] * 200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }

                else if (i == 1 && j == 8)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = App.db.absatzAllePerioden * 200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }

                else if (i == 5 && j == 8)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y = App.db.absatzAktPeriode * 200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }

                else if (i == 9 && j == 8)
                {
                    try
                    {
                        DataPoint<string, double> dp = new DataPoint<string, double>();
                        dp.X = stackAxisLbl[j];
                        dp.Y =absatz[0]*200+absatz[1]*200+absatz[2]*200;
                        ds.Add(dp);
                    }
                    catch (Exception e) { };
                }
               
                else{
                    DataPoint<string, double> dp = new DataPoint<string, double>();
                    dp.X = stackAxisLbl[j];
                    dp.Y = -1000;

                    ds.Add(dp);
                }


            }
            if (i == 9)
            {
                SolidColorBrush color = new SolidColorBrush();
                color.Color = System.Windows.Media.Color.FromRgb(11, 111, 111);
                cs.PointFill = color;
            }
            cs.LabelMaxFontSize = 6;
            cs.LabelDistance = 0.1;
            cs.ShowDataLabels = true;
          
           
            cs.DataSeries = ds;
            stack.Series.Add(cs);
            lstColSeries.Add(cs);
            
        }

        pChart.Series.Add(stack);

        LineSeries ls1 = new LineSeries();

        DataSeries<string, double> dsls = new DataSeries<string, double>();

        DataPoint<string, double> dpls = new DataPoint<string, double>();
        DataPoint<string, double> dpls1 = new DataPoint<string, double>();
        DataPoint<string, double> dpls2 = new DataPoint<string, double>();
        DataPoint<string, double> dpls3 = new DataPoint<string, double>();

        try
        {
            dpls.X = umsatz + " n-1";
            dpls.Y = App.db.absatzAktPeriode * 200 + App.db.absatzAllePerioden * 200;
            dpls1.X = umsatz + " n";
            dpls1.Y = App.db.absatzAktPeriode * 200 + App.db.absatzAllePerioden * 200 + absatz[0] * 200 + absatz[1] * 200 + absatz[2] * 200;
            dsls.Add(dpls);
            dsls.Add(dpls1);
            ls1.ShowDataLabels = true;
            ls1.DataSeries = dsls;
            ls1.ShowLine = false;
            pChart.Series.Add(ls1);
        }
        catch (Exception e) { };
        DoubleRange rng = new DoubleRange();
        rng.Minimum=0;
        try
        {
            rng.Maximum = App.db.absatzAktPeriode * 200 + App.db.absatzAllePerioden * 200 + absatz[0] * 200 + absatz[1] * 200 + absatz[2] * 200 + 100000;
        }
        catch (Exception e) { };
        pChart.YAxis.Range = rng;
        pChart.YAxis.Title = umsatz + " [EUR]";
        pChart.LegendVisibility = System.Windows.Visibility.Hidden;
        return pChart;
       }

       public class ExportReport
       {

           public static PngBitmapEncoder getImageFromControl(Control controlToConvert)
           {
               // save current canvas transform
               Transform transform = controlToConvert.LayoutTransform;

               // get size of control
               System.Windows.Size sizeOfControl = new System.Windows.Size(controlToConvert.ActualWidth + 50, controlToConvert.ActualHeight + 100);
               // measure and arrange the control
               controlToConvert.Measure(sizeOfControl);
               // arrange the surface
               controlToConvert.Arrange(new Rect(sizeOfControl));

               // craete and render surface and push bitmap to it
               RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
               // now render surface to bitmap
               renderBitmap.Render(controlToConvert);

               // encode png data
               PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
               // puch rendered bitmap into it
               pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));

               // return encoder
               return pngEncoder;
           }

           /// <summary>
           /// Get an ImageSource of a control
           /// </summary>
           /// <param name="controlToConvert">The control to convert to an ImageSource</param>
           /// <returns>The returned ImageSource of the controlToConvert</returns>
           public static ImageSource GetImageOfControl(Control controlToConvert)
           {
               // return first frame of image 
               return getImageFromControl(controlToConvert).Frames[0];
           }

           /// <summary>
           /// Save an image of a control
           /// </summary>
           /// <param name="controlToConvert">The control to convert to an ImageSource</param>
           ///  /// <param name="fileName">The location to save the image to</param>
           /// <returns>The returned ImageSource of the controlToConvert</returns>
           public static Boolean saveImageOfControl(Control controlToConvert, String fileName)
           {
               try
               {
                   // create a file stream for saving image
                   using (System.IO.FileStream outStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
                   {
                       // save encoded data to stream
                       getImageFromControl(controlToConvert).Save(outStream);
                   }
               }
               catch (Exception e)
               {
                   // display for debugging
                   Console.WriteLine("Exception caught saving stream: {0}", e.Message);
                   // return fail
                   return false;
               }

               // return that passed
               return true;
           }

           
       }
   }
}
